﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;

public partial class Manager_OrderDetail_OrderDetailOK : System.Web.UI.Page
{
    private int pQuantity = 0;
    private decimal pTotal = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminUserID"] == null)
        {
            Response.Write("<script>alert('Bạn cần đăng nhập lại để có thể tiếp tục');window.close();</script>");

            return;
        }
        /* else
         {
             clsWebShop cls = new clsWebShop();
             bool fLog = cls.IsShowPrintAutNoneProc(Session["AdminUserID"].ToString());
             if (fLog == false)
             {
                 Response.Write("<script>alert('Bạn không có quyền vào trang này');window.close();</script>");
                 return;
             }
         }
         */
        Ajax.Utility.RegisterTypeForAjax(typeof(Manager_OrderDetail_OrderDetailOK));

        if (!IsPostBack)
        {
            BindData();
        }
        string day=System.DateTime.Now.Day<10?"0"+System.DateTime.Now.Day.ToString():System.DateTime.Now.Day.ToString();
        string month = System.DateTime.Now.Month < 10 ? "0" + System.DateTime.Now.Month.ToString() : System.DateTime.Now.Month.ToString();
        lblDate.Text = "Ngày " + day + " tháng " + month + " năm " + System.DateTime.Now.Year;
    }



    private void BindData()
    {
        clsWebShop cls = new clsWebShop();
        clsCommon clsCom = new clsCommon();
        DataTable dt;
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("StartDate", "");
        msg.CreateFirstNode("EndDate", "");
        msg.CreateFirstNode("Status", "");
        msg.CreateFirstNode("End_MK", "N");
        msg.CreateFirstNode("OrderID", Request.QueryString["sOrderID"]);
        dt = cls.Get_OrderPDByCondition(msg.GetXmlStr.ToString()).Tables[0];


        if (dt.Rows.Count > 0)
        {
            lblMaDH.Text = dt.Rows[0]["OrderID"].ToString().Substring(4, dt.Rows[0]["OrderID"].ToString().Length-4);
            lblTenKH.Text = dt.Rows[0]["CusNm"].ToString();
            // lblTaxNo.Text = clsCom.FormatDate(dt.Rows[0]["BirthDay"].ToString());
            lblDiaChi.Text = dt.Rows[0]["Address"].ToString();
            lblNoichon.Text = dt.Rows[0]["PlaceNm"].ToString();
            lblSDT.Text = dt.Rows[0]["Phone"].ToString();
            lblEmail.Text = dt.Rows[0]["Email"].ToString();
            lblTaxNo.Text = dt.Rows[0]["TaxNo"].ToString();
            if (lblTaxNo.Text.Trim() == "")
            {
                trTaxNo.Visible = false;
            }
            lblQuantity.Text = FormatMoney(dt.Rows[0]["SumQuantity"].ToString());
            lblTotal.Text = FormatMoney(dt.Rows[0]["SumTotal"].ToString());

            if (dt.Rows[0]["Repaid"].ToString() == dt.Rows[0]["SumTotal"].ToString() && dt.Rows[0]["RemainPrice"].ToString() == "0")
            {
                td1.Visible = false;
                td2.Visible = false;
                td3.Visible = false;
                td4.Visible = false;
                td5.Visible = false;
                td6.Visible = false;
            }
            else
            {
                lblRepaid.Text = FormatMoney(dt.Rows[0]["Repaid"].ToString());
                lblRemainPrice.Text = FormatMoney(dt.Rows[0]["RemainPrice"].ToString());
            }


            if (dt.Rows[0]["Note"].ToString().Trim() != "")
            {
                lblChuY.Visible = true;
                lblNote.Text = dt.Rows[0]["Note"].ToString();
            }


            grvMaster.DataSource = dt;
            grvMaster.DataBind();
            /*decimal TotalQuantity = 0;
            decimal TotalPrice = 0;
            for (int i = 0; i < grvMaster.Rows.Count; i++)
            {
                TotalQuantity += decimal.Parse(grvMaster.Rows[i].Cells[2].Text.ToString());
                TotalPrice += decimal.Parse(grvMaster.Rows[i].Cells[4].Text.ToString());
            }
            lblTongsosach.Text = FormatMoney(TotalQuantity);
            lblTongsotien.Text = FormatMoney(TotalPrice);*/

            lblUSD.Text = "<b>Tỉ giá USD:</b> " + FormatMoney(dt.Rows[0]["USD"].ToString()) + " VNĐ";

            System.Web.UI.WebControls.Image myImage = new System.Web.UI.WebControls.Image();
            /*  myImage.Width = Unit.Pixel(100);
              myImage.Height = Unit.Pixel(100);*/
            myImage.ImageUrl = "../Signature/UserSignImages.aspx";
            pnlSign.Controls.Add(myImage);
            lblSignNm.Text = Session["CusName"].ToString();

        }
    }

    protected void grvMaster_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            pQuantity += ConvertToInt(e.Row.Cells[2].Text);
            pTotal+= ConvertToDecimal(e.Row.Cells[4].Text);
            Label lblDonGia = (Label)e.Row.FindControl("lblDonGia");
            e.Row.Cells[4].Text = FormatMoney(e.Row.Cells[4].Text);
            lblDonGia.Text = FormatMoney(lblDonGia.Text);
        }
        else if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[0].Text = "Tổng cộng:";
            e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
            e.Row.Cells[0].ColumnSpan = 2;
           

            e.Row.Cells[2].Text = pQuantity.ToString();
            e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Center;
            e.Row.Cells[4].Text = FormatMoney(pTotal.ToString())+ " VNĐ";
            e.Row.Cells.RemoveAt(1);
        }
    }

    private int ConvertToInt(string Quantity)
    {
        int dQuantity = 0;
        try
        {
            dQuantity = int.Parse(Quantity);
        }
        catch
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Số lượng không hợp lệ')</script>");

        }
        return dQuantity;
    }

    private decimal ConvertToDecimal(string Quantity)
    {
        decimal dQuantity = 0;
        try
        {
            dQuantity = decimal.Parse(Quantity);
        }
        catch
        {
            return 0;
        }
        return dQuantity;
    }


    [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)]
    public string AjaxUpdateOrderState(string sState, string sOrder)
    {
        clsWebShop cls = new clsWebShop();
        string myMsg = "";
        switch (sState.ToLower())
        {
            case "save":
                cls.UpdateData("update OrderPD set Status='Y' where  OrderID='" + sOrder + "'");
                myMsg = "Y";
                break;
            case "cancel":
                cls.UpdateData("update OrderPD set Status='N' where  OrderID='" + sOrder + "'");
                myMsg = "N";
                break;
        }
        return myMsg;
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        Response.Redirect("OrderDetail.aspx?CodeID=" + Request.QueryString["CodeID"] + "&sDate=" + Request["sDate"].ToString() + "&eDate=" + Request["eDate"].ToString() + "&sDrop=" + Request["sDrop"].ToString() + "");

    }
    private string FormatMoney(decimal dNum)
    {
        string sReturn = "0";
        try
        {
            sReturn = string.Format("{0:###,###,##0}", dNum);
        }
        catch (Exception) { }
        return sReturn;
    }

    private string FormatMoney(string sNum)
    {
        string sReturn = "0";
        try
        {
            sReturn = string.Format("{0:###,###,##0}", decimal.Parse(sNum));
        }
        catch (Exception) { }
        return sReturn;
    }
}
